Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example of building a Dockerfile with local tar file build context #702

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

njlr
Copy link

@njlr njlr commented Sep 21, 2024

This PR adds an example of using a Dockerfile with a local tar as a build context.

This is not the ideal approach, which this is clearly signposted in the README!, but it shows an easy migration path from rules_docker. The existing example does not show how Bazel build artefacts can be passed to BuildX.

How it works:

  1. Create a tar file using rules_pkg
  2. Pipe the tar file into BuildX using sh_binary and run_binary
  3. Use the BuildX output as a base for an oci_image

This is a replacement for container_run_and_commit.

The example could easily be adapted to behave like container_run_and_extract by changing the --output flag to a tar.

@thesayyn
Copy link
Collaborator

We have https://github.com/bazel-contrib/rules_oci/tree/main/examples/dockerfile, how is this different than that one?

@njlr
Copy link
Author

njlr commented Sep 21, 2024

We have https://github.com/bazel-contrib/rules_oci/tree/main/examples/dockerfile, how is this different than that one?

That example does not show how to use the output of other Bazel targets as input to the Dockerfile build

@thesayyn
Copy link
Collaborator

That example does not show how to use the output of other Bazel targets as input to the Dockerfile build

Than can we change it so it does? i don't see the difference between this example and the other one, other than putting the srcs into a tar file beforehand so you don't have to deal with paths that start with bazel-bin/cfg/bin

@njlr
Copy link
Author

njlr commented Sep 21, 2024

That example does not show how to use the output of other Bazel targets as input to the Dockerfile build

Than can we change it so it does? i don't see the difference between this example and the other one, other than putting the srcs into a tar file beforehand so you don't have to deal with paths that start with bazel-bin/cfg/bin

Yes, happy to do this.

The reason I made a new example is that it's a little more complicated than the existing example, so might hinder understanding.

What do you think?

@thesayyn
Copy link
Collaborator

We can add some comments explaining why there is two different buildx targets in the same package and the difference.

@njlr
Copy link
Author

njlr commented Oct 16, 2024

We can add some comments explaining why there is two different buildx targets in the same package and the difference.

I have merged the two examples

Copy link
Collaborator

@thesayyn thesayyn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@thesayyn
Copy link
Collaborator

There are some failures, can you take a look?

name = "buildx_context_tree_prefixed",
mtree = ":buildx_context_tree",
strip_prefix = package_name(),
package_dir = "/app",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need a ./app to fix the issue.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - this works

One thing I found a little surprising is that I need to add the srcs for both the mtree_spec and the tar.

Is this expected? Am I using it correctly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants